

package dump;

import java.util.Formatter.DateTime;

import system.*
import app.*;

class DumpService{

	File file;

	def dumpFileDirectory = Constants.dumpFileDirectory;
	def dumpFileExt = Constants.dumpFileExt
	def dbName = Constants.dbName
	
	def shellFileDirectory = Constants.shellFileDirectory;
	def shellFileExt = Constants.shellFileExt
	
	def shellBackupFileName = Constants.shellBackupFileName
	def shellRestoreFileName = Constants.shellRestoreFileName



	public String backup() {
		
		String cmd = SystemCommand.execute("sh ${Utils.getFilePath(shellFileDirectory, shellBackupFileName, shellFileExt)}")//pg_dump ${dbName} > ${dbName}_123456.${fileExt}");//"`eval date +%y%m%d_%H%M%S`."+fileExt);
		println(cmd);
		return cmd;
	}
	
	public String restore(String fileName) {
		
		/*Load restore.sh file content*/
		File restoreScript = new File(Utils.getFilePath(shellFileDirectory, shellRestoreFileName, shellFileExt))
		def lines = restoreScript.readLines()
		
		/*Delete restore.sh file in scripts directory if exists*/
		restoreScript.delete()

		
		def regex = 'file_name="SugarBindDB_'
		def newFileName = 'file_name="'+fileName+'"' 
			
		/*Modifiy the target filename with the selected one on web page and write to restore.sh file*/
		lines.each {
				if(it.contains(regex))
				{
					it = newFileName;
				}
				def newLine = it+System.getProperty("line.separator")
				
				println("new line : "+newLine)
				restoreScript << (newLine)
			}
			
				
		/*Execute*/
		String execCmd = SystemCommand.execute("sh ${Utils.getFilePath(shellFileDirectory, shellRestoreFileName, shellFileExt)}")
		println(execCmd)
		return execCmd
		
		}

}

